package smodul

import "github.com/jinzhu/gorm"

type BroadcastManage struct {
	gorm.Model
	Rid uint32 `gorm:"not null"`
	CourseName string `gorm:"type:varchar(255);not null"`
}

func (b * BroadcastManage)SelectByCourseName(cname string)error{
	return MysqlDB.Where("course_name = ?",cname).Find(b).Error
}

func AddBroadcastCourse(rid uint32,cname string )*BroadcastManage{
	if s:=MysqlDB.Where("rid = ? and course_name = ?",rid,cname).First(new(BroadcastManage)).RowsAffected;s >0{
		return nil
	}
	bcm := & BroadcastManage{
		Model:      gorm.Model{},
		Rid:        rid,
		CourseName: cname,
	}
	if count:= MysqlDB.Create(bcm).RowsAffected;count<=0{
		return nil
	}
	return bcm
}

func GetAllBroadcastCourse(rid uint32)([]string,error){
	list := make([]string,0)
	err := MysqlDB.Where("rid = ?",rid).Find(&list).Error
	return list,err
}